package com.quarkus.cloud.controller;

import java.io.File;
import java.net.URLEncoder;
import java.nio.charset.StandardCharsets;

import org.apache.commons.io.FileUtils;
import org.jboss.resteasy.annotations.providers.multipart.MultipartForm;

import com.quarkus.cloud.common.util.BaseResponse;
import com.quarkus.cloud.domain.request.FormData;

import jakarta.ws.rs.Consumes;
import jakarta.ws.rs.GET;
import jakarta.ws.rs.POST;
import jakarta.ws.rs.Path;
import jakarta.ws.rs.Produces;
import jakarta.ws.rs.QueryParam;
import jakarta.ws.rs.core.MediaType;
import jakarta.ws.rs.core.Response;
import lombok.extern.slf4j.Slf4j;

/**
 * @author zhuhb-a#glodon.con
 * @Description 描述
 * @date 2024/4/3010:20
 */
@Path("/attachment")
@Slf4j
public class AttachmentController {
    @POST
    @Path("upload")
    @Produces(MediaType.APPLICATION_JSON)
    @Consumes(MediaType.MULTIPART_FORM_DATA)
    public BaseResponse<String> list(@MultipartForm FormData formData)   {
        File file = formData.getFile();
        String fileName = formData.getFileName();
        log.info("上传文件名:{}", fileName);
        log.info("表单提交的文件名:{}", formData.getFileName());
        return BaseResponse.success("成功");
    }

    @GET
    @Path("/download")
    @Produces(MediaType.APPLICATION_OCTET_STREAM)
    public Response download(@QueryParam("fileName") String fileName) {
        String filePath = "E:\\工作相关\\" + fileName;
        File file = FileUtils.getFile(filePath);
        //对输出的文件名进行编码，防止下载的中文文件名乱码
        String encodFileName = URLEncoder.encode(fileName, StandardCharsets.UTF_8);
        return Response.ok(file).header("content-disposition", "attachment; filename=\"" + encodFileName + "\"").header("Content-Length", file.length()).build();
    }
}
